local lingxie = fk.CreateSkill{
    name = "wk_heg__lingxie",
}

Fk:loadTranslationTable{
    ["wk_heg__lingxie"] = "令协",
    [":wk_heg__lingxie"] = "每回合结束时，若你此回合内受到过伤害，你可令两名你势力相同的角色交换副将并重新结算对应的国战标记。",

    ["#wk_heg__lingxie-choose"] = "令协：选择两名与你势力相同的角色交换副将",

    ["$wk_heg__lingxie1"] = "分则必败，合则可胜！",
    ["$wk_heg__lingxie2"] = "唯同心协力，方可破敌。",
}

local H = require "packages.ol_hegemony.util"
local HG = require "packages.new_heg_diy_my.util"

lingxie:addEffect(fk.TurnEnd,{
    anim_type = "special",
    can_trigger = function(self, event, target, player, data)
      if not (player:hasSkill(lingxie.name) and table.find(player.room.alive_players, function (p) return H.compareKingdomWith(p, player) and p ~= player end)) then return false end
      local targets = {}
      player.room.logic:getActualDamageEvents(1, function(e)
        local damage = e.data
        if damage.to == player then
          table.insertIfNeed(targets, damage.to)
        end
        return false
      end)
      return #targets > 0
    end,
    on_use = function(self, event, target, player, data)
      local room = player.room
      local targets = table.filter(room.alive_players, function (p) return H.compareKingdomWith(p, player) end)
      local tos = room:askToChoosePlayers(player,{
        targets = targets,
        min_num = 2,
        max_num = 2,
        prompt = "#wk_heg__lingxie-choose",
        skill_name = lingxie.name,
        cancelable = false,
      })
      local to1 = tos[1]
      local to2 = tos[2]
      H.SwapTwoPlayersDeputy(room, to1, to2)
      local to1general = Fk.generals[to1.general]
      local to1deputy = Fk.generals[to1.deputyGeneral]
      local to2general = Fk.generals[to2.general]
      local to2deputy = Fk.generals[to2.deputyGeneral]

      if ((to1general.maxHp + to1deputy.maxHp) % 2 == 1) and to1:getMark("@!!yinyangfish") == 0 then
        H.addHegMark(room, to1, "yinyangfish")
      end
      if to1general:isCompanionWith(to1deputy) and to1:getMark("@!!companion") == 0 then
        H.addHegMark(room, to1, "companion")
      end
      if ((to2general.maxHp + to2deputy.maxHp) % 2 == 1) and to2:getMark("@!!yinyangfish") == 0 then
        H.addHegMark(room, to2, "yinyangfish")
      end
      if to2general:isCompanionWith(to2deputy) and to2:getMark("@!!companion") == 0 then
        H.addHegMark(room, to2, "companion")
      end
    end,
})

return lingxie